#include <QtGui>
#include "classConcreteDialog.h"

ClassConcreteDialog::ClassConcreteDialog(QWidget *parent) : QTableWidget(parent) {

//Кодек для руссификации
  russianCodec = QTextCodec::codecForName("UTF-8");
  QTextCodec::setCodecForTr(russianCodec);
  QTextCodec::setCodecForCStrings(russianCodec);

//Установки для окна
  this->setWindowTitle(tr("Определение допустимой скорости"));
  this->setFixedSize(WCLCONCRETE, HCLCONCRETE);
  // this->resize(WCLCONCRETE, HCLCONCRETE);

  this->verticalHeader()->hide();
  this->horizontalHeader()->setEnabled(false);
  this->setColumnCount(6);
  this->setRowCount(6);

  this->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
  this->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
  this->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
  this->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed);
  this->horizontalHeader()->setResizeMode(4, QHeaderView::Fixed);
  this->horizontalHeader()->setResizeMode(5, QHeaderView::Stretch);

  curSelRow = -1;

  //Задает имена колонкам в таблице переменных
  nameColumn = new QTableWidgetItem();
  nameColumn->setText(tr("Марка"));
  nameColumn->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setHorizontalHeaderItem(0, nameColumn);

  nameColumn = new QTableWidgetItem();
  nameColumn->setText(tr("Прочность"));
  nameColumn->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setHorizontalHeaderItem(1, nameColumn);

  nameColumn = new QTableWidgetItem();
  nameColumn->setText(tr("Hrain=0.6"));
  nameColumn->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setHorizontalHeaderItem(2, nameColumn);

  nameColumn = new QTableWidgetItem();
  nameColumn->setText(tr("Hrain=1.0"));
  nameColumn->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setHorizontalHeaderItem(3, nameColumn);

  nameColumn = new QTableWidgetItem();
  nameColumn->setText(tr("Hrain=3.0"));
  nameColumn->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setHorizontalHeaderItem(4, nameColumn);

  nameColumn = new QTableWidgetItem();
  nameColumn->setText(tr("Hrain=6.0"));
  nameColumn->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setHorizontalHeaderItem(5, nameColumn);

  this->setColumnWidth(0, 100);
  this->setColumnWidth(1, 100);
  this->setColumnWidth(2, 100);
  this->setColumnWidth(3, 100);
  this->setColumnWidth(4, 100);
  // this->setColumnWidth(5, 100);

  for (int i=0; i < CLCMAXITEMS; i++)
    for (int j=0; j < CLCMAXITEMS; j++)
      items[i][j] = NULL;

//СОЗДАНИЕ ЭЛЕМЕНТОВ

  int curRow;

//1
  curRow = 0;

  createItem(curRow, 0, tr("B2.5"), true);

  createItem(curRow, 1, "50", true);
  createItem(curRow, 2, "0.6", true);
  createItem(curRow, 3, "10.6", true);
  createItem(curRow, 4, "12.3", true);
  createItem(curRow, 5, "13.0", true);

  this->setRowHeight(curRow, 25);

//2
  curRow = 1;

  createItem(curRow, 0, tr("B5"), true);

  createItem(curRow, 1, "75", true);
  createItem(curRow, 2, "11.2", true);
  createItem(curRow, 3, "12.4", true);
  createItem(curRow, 4, "14.3", true);
  createItem(curRow, 5, "15.2", true);

  this->setRowHeight(curRow, 25);

//3
  curRow = 2;

  createItem(curRow, 0, tr("B7.5"), true);

  createItem(curRow, 1, "100", true);
  createItem(curRow, 2, "12.5", true);
  createItem(curRow, 3, "13.8", true);
  createItem(curRow, 4, "16.0", true);
  createItem(curRow, 5, "17.0", true);

  this->setRowHeight(curRow, 25);

//4
  curRow = 3;

  createItem(curRow, 0, tr("B10"), true);

  createItem(curRow, 1, "150", true);
  createItem(curRow, 2, "14.0", true);
  createItem(curRow, 3, "15.6", true);
  createItem(curRow, 4, "18.0", true);
  createItem(curRow, 5, "19.1", true);

  this->setRowHeight(curRow, 25);

//5
  curRow = 4;

  createItem(curRow, 0, tr("B12.5"), true);

  createItem(curRow, 1, "200", true);
  createItem(curRow, 2, "15.6", true);
  createItem(curRow, 3, "17.3", true);
  createItem(curRow, 4, "20.0", true);
  createItem(curRow, 5, "21.2", true);

  this->setRowHeight(curRow, 25);

//6
  curRow = 5;

  createItem(curRow, 0, tr("B15"), true);

  createItem(curRow, 1, "300", true);
  createItem(curRow, 2, "19.2", true);
  createItem(curRow, 3, "21.2", true);
  createItem(curRow, 4, "24.6", true);
  createItem(curRow, 5, "26.1", true);

  this->setRowHeight(curRow, 25);

//Сигналы выделения элемента
  connect(this, SIGNAL(cellClicked(int, int)), this, SLOT(selectCell(int, int)));
  connect(this, SIGNAL(cellActivated(int, int)), this, SLOT(selectCellAndClose(int, int)));

} //ClassConcreteDialog::ClassConcreteDialog(QWidget *parent) : QWidget(parent)

bool ClassConcreteDialog::createItem(int row, int column, const QString & str, bool center) {

  if ((row < CLCMAXITEMS) && (column < CLCMAXITEMS)) {

    item = new QLabel();
    item->setAutoFillBackground(true);
    item->setText(str);
    item->setIndent(CLCITEMINDENT);
    if (center)
      item->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    this->setCellWidget(row, column, item);
    items[row][column] = item;
    setDefColor(row, column);

    return true;
    
  } //if ((row < CLCMAXITEMS) && (column < CLCMAXITEMS))

  else {

    return false;

  } //else: if ((row < CLCMAXITEMS) && (column < CLCMAXITEMS))

} //bool ClassConcreteDialog::createItem(int row, int column, const QString & str, bool center)

void ClassConcreteDialog::selectCell(int row, int ) {

  int red   = 200;
  int green = 200;
  int blue  = 230;

  // int red   = 150;
  // int green = 150;
  // int blue  = 230;

  resetSel();
  setColor(QColor(red, green, blue), row, 0);
  setColor(QColor(red, green, blue), row, 1);
  setColor(QColor(red, green, blue), row, 2);
  setColor(QColor(red, green, blue), row, 3);
  setColor(QColor(red, green, blue), row, 4);
  setColor(QColor(red, green, blue), row, 5);

  int num = 0;

  switch (row) {

    case 0:          num =  0; break;
    case 1:          num =  1; break;
    case 2:          num =  2; break;
    case 3:          num =  3; break;
    case 4:          num =  4; break;
    case 5:          num =  5; break;

  } //switch (row)

  emit selectClass(num+1);

  curSelRow = row;

} //void ClassConcreteDialog::selectCell(int row, int column)

void ClassConcreteDialog::selectCellAndClose(int row, int ) {

  selectCell(row, 0);

  this->close();

} //void ClassConcreteDialog::selectCellAndClose(int row, int column)

void ClassConcreteDialog::setColor(const QColor & color, int row, int column) {

  palette.setColor(QPalette::Base, color); 
  items[row][column]->setPalette(palette);

} //void ClassConcreteDialog::setColor(const QColor & color, int num)

void ClassConcreteDialog::setDefColor(int row, int column) {

  if (row % 2 == 0)
    setColor(QColor(240, 240, 240), row, column);
  else
    setColor(QColor(230, 230, 230), row, column);

} //void ClassConcreteDialog::setDefColor(int num)

void ClassConcreteDialog::resetSel() {

  curSelRow = -1;

  for (int i=0; i < CLCMAXITEMS; i++)
    for (int j=0; j < CLCMAXITEMS; j++)
      if (items[i][j] != NULL)
	setDefColor(i, j);

} //void ClassConcreteDialog::resetSel()

void ClassConcreteDialog::keyPressEvent(QKeyEvent * event) {

  if (event->key() == Qt::Key_Up) {
  
    if (curSelRow > 0)
      selectCell(curSelRow-1, 0);
    else
      selectCell(0, 0);

  } //if (event->key() == Qt::Key_Up)  

  else if (event->key() == Qt::Key_Down) {

    if (curSelRow < 5)
      selectCell(curSelRow+1, 0);

  } //else if (event->key() == Qt::Key_Down)

  else if (event->key() == Qt::Key_Return) {

    if (curSelRow != -1)
      selectCellAndClose(curSelRow, 0);

  } //else if (event->key() == Qt::Key_Return)

  else if (event->key() == Qt::Key_Escape) {

    this->hide();

  } //else if (event->key() == Qt::Key_Return)

} //void ClassConcreteDialog::keyPressEvent(QKeyEvent * event)
